package it.controllocredito.frontend.activities;

import java.util.ArrayList;
import java.util.List;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.bean.RegistroChiamate;
import it.controllocredito.backend.phone.calllog.ManagerCallLogHandler;
import it.controllocredito.backend.utility.Utilities;
import it.controllocredito.frontend.util.RegistroChiamateArrayAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class RegistroChiamateActivity extends SherlockFragmentActivity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.registro_chiamate_activity);

		CalllogPersistenceManager calllogPersistenceManager = new CalllogPersistenceManager(this.getApplicationContext());
		final List<RegistroChiamate> listaRegistroChiamate = calllogPersistenceManager.getRecordsRegistroChiamate();

		final RegistroChiamateArrayAdapter registroChiamateArrayAdapter = new RegistroChiamateArrayAdapter(getApplicationContext(), listaRegistroChiamate);

		final ListView listView = (ListView) findViewById(R.id.mylist);

		// Assign adapter to ListView
		listView.setAdapter(registroChiamateArrayAdapter);
		
		final TextView textLabelTot = (TextView) findViewById(R.id.tot_uscita);
		textLabelTot.setText(Utilities.calculateTotal(listaRegistroChiamate, CallLog.Calls.OUTGOING_TYPE));
		
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
		        final RegistroChiamate item = (RegistroChiamate) parent.getItemAtPosition(position);
		        listaRegistroChiamate.remove(item);
        		textLabelTot.setText(Utilities.calculateTotal(listaRegistroChiamate, CallLog.Calls.OUTGOING_TYPE));
		        registroChiamateArrayAdapter.notifyDataSetChanged();
		      }
		});

		// FINE LISTVIEW
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainFragmentActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}